package com.luxoft.scoremanager.server.model.dao;


import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;


@Entity
@Table
public class ComponentDaoModel implements Serializable {
    private static final long serialVersionUID = -5170875020617735663L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "my_entity_seq_gen")
    @SequenceGenerator(name = "my_entity_seq_gen", sequenceName = "CD_SEQ")
    @Column(name = "component_ID")
    private Integer id;

    @Column
    private String label;

    @Column
    private String staticData;

    @OneToMany(mappedBy = "componentDaoModel", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @Fetch(value = FetchMode.SUBSELECT)
    private List<ComponentValuesDaoModel> componentsValues = new ArrayList<ComponentValuesDaoModel>();

    public ComponentDaoModel() {

    }

    public ComponentDaoModel(String label, String staticData) {
        this.label = label;
        this.staticData = staticData;
    }

    public ComponentDaoModel(Integer id, String label, String staticData) {
        this.id = id;
        this.label = label;
        this.staticData = staticData;
    }

    public static long getSerialVersionUID() {

        return serialVersionUID;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    public String getStaticData() {
        return staticData;
    }

    public void setStaticData(String staticData) {
        this.staticData = staticData;
    }

    public List<ComponentValuesDaoModel> getComponentsValues() {
        return componentsValues;
    }

    public void setComponentsValues(List<ComponentValuesDaoModel> componentsValues) {
        this.componentsValues = componentsValues;
    }

    public void addComponentValues(ComponentValuesDaoModel componentsValues) {
        this.componentsValues.add(componentsValues);
    }

    public void deleteComponentValues(ComponentValuesDaoModel componentsValues) {
        if (this.componentsValues.contains(componentsValues)) {
            this.componentsValues.remove(componentsValues);
        }
    }

    @Override
    public boolean equals(Object o) {

        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        ComponentDaoModel that = (ComponentDaoModel) o;

        if (id != null ? !id.equals(that.id) : that.id != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        return id != null ? id.hashCode() : 0;
    }

    @Override
    public String toString() {
        return "Component{" +
                "id=" + id +
                ", label='" + label + '\'' +
                ", staticData='" + staticData + '\'' +
                '}';
    }


}